package com.hopes.bizz.close.position.support.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hopes.bizz.close.basic.entity.ClosedGpsDeviceEntity;
import com.hopes.bizz.close.common.constant.CacheConstants;
import com.hopes.bizz.close.position.dto.HazardCachePositionDTO;
import com.hopes.bizz.close.position.support.RedisHandleSupport;
import com.hopes.bizz.close.position.vo.HazardCachePositionVO;
import com.hopes.bizz.close.position.vo.SocketSendVO;
import com.hopes.bizz.close.visitor.entity.ClosedHazardCarApplyEntity;
import com.hopes.boss.common.message.config.MessageProperties;
import com.hopes.boss.common.message.distribute.IMessage;
import com.hopes.boss.common.message.distribute.IMessageDistributor;
import com.hopes.boss.common.message.distribute.MessageSendBO;
import com.hopes.boss.common.websocket.config.WebSocketProperties;
import com.hopes.boss.common.websocket.distribute.MessageDO;
import com.hopes.boss.common.websocket.distribute.MessageSender;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description Redis数据处理支撑类
 * @time 2024/1/30 16:05
 */
@Service
@RequiredArgsConstructor
public class RedisHandleSupportImpl implements MessageSender, RedisHandleSupport {

    private final MessageProperties MessageProperties;
    private final IMessageDistributor messageDistributor;

    @Override
    public void messageSend(MessageSendBO messageSend) {
        if (ObjUtil.isNull(messageSend)) {
            return;
        }
        messageDistributor.distribute(messageSend);
    }

    @Override
    public void messageSend(List<IMessage<?>> messageList) {
        if (CollUtil.isEmpty(messageList)) {
            return;
        }
        messageDistributor.distribute(new MessageSendBO().addAllMessage(messageList));
    }

    @Override
    public <T> void webSocketSend(T socketData, String wsUrl) {
        if (ObjUtil.isNull(socketData)) {
            return;
        }
        if (StrUtil.isBlank(wsUrl)) {
            wsUrl = MessageProperties.getPath();
        }
        final MessageDO messageDO = new MessageDO();
        messageDO.setNeedBroadcast(Boolean.TRUE);
        messageDO.setMessageText(JSONUtil.toJsonStr(new SocketSendVO<>(socketData)));
        doSend(messageDO);
    }

    @Override
    @CachePut(value = CacheConstants.CLOSE_HAZARD_CACHE_POSITION + "#300", key = "#hazardCachePositionDTO.deviceNo", unless = "#result == null")
    public HazardCachePositionVO buildHazardCachePosition(HazardCachePositionDTO hazardCachePositionDTO, ClosedGpsDeviceEntity gpsDevice, ClosedHazardCarApplyEntity hazardCarApply) {
        final HazardCachePositionVO cachePositionVO = new HazardCachePositionVO();
        BeanUtils.copyProperties(hazardCachePositionDTO, cachePositionVO);
        cachePositionVO.setBillNo(hazardCarApply.getBillNo());
        cachePositionVO.setHazardApplyId(hazardCarApply.getId());
        // TODO 计算当前是否在车道中、停车场、企业内 计算是否报警中
        cachePositionVO.setLicencePlateNum(hazardCarApply.getLicencePlateNum());
        cachePositionVO.setOrgId(Optional.ofNullable(hazardCarApply.getOrgId()).map(String::valueOf).orElse(""));
        return cachePositionVO;
    }
}
